package com.ctakit.kiwi.common.core.bean;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.lang3.StringUtils;

/**
 * @author lizhenmin
 */
@Slf4j
public class DefaultNullValuePropertyValueGetter {
    public static Object getProperty(Object from, String propertyName) {
        if (null == from || StringUtils.isBlank(propertyName)) {
            log.info("Object is null or the property [{}] is blank, returning null", propertyName);
            return null;
        }

        try {
            return PropertyUtils.getProperty(from, propertyName);
        } catch (Exception e) {
            log.info("Exception [{}] when fetching property [{}] from object [{}], returning null as the value.", e.toString(), propertyName, from);
            return null;
        }
    }

}
